<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use classlib\validator\auditor;
class Cvipsqua extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/vipsqua.ext.js');
	}
	
	/**
	 * @desc    保存VIP守护配置资料信息
	 * @doc     编辑/添加守护
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$data   = array();
		try {
			$data['coinprice'] = $filter->reset('coinprice')->numeric()->min(0)->run();
			$data['name'] 	   = $filter->reset('name')->required()->lbetween(1, 255)->run();
			$data['desc'] 	   = $filter->reset('desc')->required()->lbetween(1, 255)->run();
			$batchs			   = $filter->reset('batchprice')->required()->match('/^([\d]+\-[\d]+;)+$/')->run();
			//设置座驾的设置
			$motorid           = $filter->reset('motorid')->numeric()->run();
			$setting           = array();
			if ($motorid > 0) {
				$setting['motorid'] = $motorid;
			}
			$data['settings']  = json_encode($setting);
			//解析批次购买的价格配置
			$batchs     	   = @explode(';', $batchs);
			$batchprice 	   = array();
			foreach ($batchs as $strprice) {
				if ($strprice && preg_match('/^[\d]+\-[\d]+$/', $strprice)) {
					list($month, $price) = explode('-', $strprice);
					if ($month > 0 && $price > 0) {
						$batchprice[$month] = $price;
					}
				}
			}
			$data['batchprice'] = json_encode($batchprice);
			$acl  = \classlib\acl::inst();
			$res  = \src\logic\upfile::inst($acl)->post('Filedata');
			if ($res) {
				$data['iconsrc'] = $res['url'];
			}
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$id    = (int)input::get_post('id');
		$orm   = C\model::load('pro.inc.guard');
		$data['stime'] = C\config::$nowtime;
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * @desc    删除VIP守护配置
	 * @doc     删除记录
	 * @access  需要授权
	 */
	public function dodelete() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$ids    = auditor::intstr2arr('ids');
		$orm    = C\model::load('pro.inc.guard');
		foreach ($ids as $id) {
			$data  = $orm->get_one($id);
			if ($data) {
				Cadmin::log2db(C\view::inst('xml')->array2xml($data));
				$orm->delete($id);
			}
		}
		$view->display();
	}
	
	/**
	 * 获取用户资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where  = array();
		$orm    = C\model::load('pro.inc.guard');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total > 0) {
			$lorm = C\model::load('pro.pl.props');
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			$morm = C\model::load('pro.inc.motor');
			foreach ($list as &$item) {
				$setting = json_decode($item['settings'], true);
				$item['motorid'] = 0;
				if (isset($setting['motorid']) && $setting['motorid'] > 0) {
					$motor = $morm->get_one($setting['motorid']);
					$item['motor']   = empty($motor)? '未知[UnKnow]':$motor['name'];
					$item['motorid'] = $setting['motorid'];
				}
				$batchprice = json_decode($item['batchprice'], true);
				$batchs  	= array();
				if ($batchprice) {
					foreach ($batchprice as $month=>$price) {
						$batchs[] = sprintf('%d-%d', $month, $price);
					}
					$item['batchprice'] = implode(';', $batchs).';';
				}
				$item['count']= $lorm->get_total(array('reftype'=>2, 'refid'=>$item['id']));
				$item['stime']= date('Y-m-d H:i:s', $item['stime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}